package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;


    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        log.info("添加到购物车的数据：{}", shoppingCart.toString());
        //设置用户id，指定当前是哪个用户的购物车数据
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //获取菜品id
        Long dishId = shoppingCart.getDishId();
        //创建购物车条件构造器
        LambdaQueryWrapper<ShoppingCart> shoppingCartWrapper = new LambdaQueryWrapper<>();
        //条件
        shoppingCartWrapper.eq(ShoppingCart::getUserId, userId);
        //判断是菜品还是套餐
        if (dishId != null) {
            //添加到购物车的是菜品
            shoppingCartWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            shoppingCartWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //查询当前菜品或者套餐是否在购物车中
        ShoppingCart shoppingCartOne = shoppingCartService.getOne(shoppingCartWrapper);
        if (shoppingCartOne != null && shoppingCartOne.getNumber() >= 1) {
            //如果已经存在，就在原来数量基础上减一
            Integer number = shoppingCartOne.getNumber();
            shoppingCartOne.setNumber(number - 1);
            shoppingCartService.updateById(shoppingCartOne);
        }
        if (shoppingCartOne.getNumber()==0){
            shoppingCartService.remove(shoppingCartWrapper);
        }
        return R.success(shoppingCartOne);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        //创建购物车条件构造器
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //条件：查询用户id比较
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(wrapper);
        return R.success("清空购物车成功");
    }

    /**
     * 查询购物车
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        //创建购物车条件构造器
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //条件：查询用户
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //条件：排序
        wrapper.orderByDesc(ShoppingCart::getCreateTime);
        //查询所有购物车信息
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(wrapper);
        //返回购物车数据
        return R.success(shoppingCartList);
    }

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("添加到购物车的数据：{}", shoppingCart.toString());
        //设置用户id，指定当前是哪个用户的购物车数据
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //获取菜品id
        Long dishId = shoppingCart.getDishId();
        //创建购物车条件构造器
        LambdaQueryWrapper<ShoppingCart> shoppingCartWrapper = new LambdaQueryWrapper<>();
        //条件
        shoppingCartWrapper.eq(ShoppingCart::getUserId, userId);
        //判断是菜品还是套餐
        if (dishId != null) {
            //添加到购物车的是菜品
            shoppingCartWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            shoppingCartWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //查询当前菜品或者套餐是否在购物车中
        ShoppingCart shoppingCartOne = shoppingCartService.getOne(shoppingCartWrapper);
        if (shoppingCartOne != null) {
            //如果已经存在，就在原来数量基础上加一或减一
            Integer number = shoppingCartOne.getNumber();
            shoppingCartOne.setNumber(number + 1);
            shoppingCartService.updateById(shoppingCartOne);
        } else {
            //如果不存在，则添加到购物车，数量默认就是一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            //赋值
            shoppingCartOne = shoppingCart;
        }
        return R.success(shoppingCartOne);
    }
}
